package cn.com.oceansoft.business.szjf.service.impl;

import cn.com.oceansoft.business.log.service.IOperLogService;
import cn.com.oceansoft.business.sys.entity.SysUser;
import cn.com.oceansoft.business.sys.persistence.ISysUserMapper;
import cn.com.oceansoft.business.szjf.entity.*;
import cn.com.oceansoft.business.szjf.persistence.IExpertMapper;
import cn.com.oceansoft.business.szjf.persistence.IPszlMapper;
import cn.com.oceansoft.business.szjf.persistence.ISmsMapper;
import cn.com.oceansoft.business.szjf.persistence.IYsshMapper;
import cn.com.oceansoft.business.szjf.service.IExpertService;
import cn.com.oceansoft.business.szjf.service.ISmsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/7/4.
 */
@Service
@Transactional
public class ExpertService implements IExpertService {
    @Resource
    private IExpertMapper expertMapper;
    @Resource
    private ISysUserMapper userMapper;
    @Resource
    private IPszlMapper pszlMapper;
    @Resource
    private IYsshMapper ysshMapper;
    @Resource
    private ISmsService smsService;
    @Resource
    private IOperLogService operLogService;

    /**
     * 获取专家信息列表
     * @param a A类或者B类
     * @param free free 表示空闲状态，否则是全部
     * @return
     */
    @Override
    public List<Expert> getExperts(Expert a, String free) {
        List<Expert> experts = null;
        if(free.equals("free")){
             experts = expertMapper.getFreeExperts(a);
        }else{

        }
        return experts;
    }

    /**
     * 更新表中被抽取记录数与抽取后缺席的次数
     * @param expert
     */
    @Override
    public void updateUserExtension(Expert expert) {
        expertMapper.updateUserExtension(expert);
    }

    /**
     * 插入数据
     * @param expert
     */
    @Override
    public void insertExpertsMap(Expert expert) {

        try {

            //获取专家详细信息
            SysUser user = userMapper.findById(expert.getGuid());
            if(user != null){
                expert.setPaguid(expert.getRemark());//存放审评或验收的guid
                expert.setCompany(user.getCompany());//公司
                expert.setJob(user.getJob());//专家
                expert.setCreatetime(new Date());
                expert.setIsleave(expert.getIsleave()==null || expert.getIsleave()==0? 0:1);//0出席，1不出席
                expertMapper.insertExpertsMap(expert);
            }
        }catch (Exception e){

            e.printStackTrace();
        }

    }

    /**
     * 插入数据
     * @param expert
     * @param flag
     */
    @Override
    public void insertZjBak(Expert expert, String flag) {

        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            expert.setTdate(sdf.format(new Date()));
            expert.setTemp2(expert.getIsleave()==1? "1":"2");//1是不参加，2是参加  type字段
            if(expert.getIsleave()!=1){
                if("pssh".equals(flag)){
                    expert.setRemark("prjGuid:"+expert.getRemark());
                }else if("yssh".equals(flag)){
                    expert.setRemark("accGuid:"+expert.getRemark());
                }
            }
            expertMapper.insertZjBak(expert);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void insertCheckInfo(CheckInfo info) {
        try {
            expertMapper.insertCheckInfo(info);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteExpertsTemp(String paguid) {
        try {
            expertMapper.deleteExpertsTemp(paguid);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<Expert> getExpertsPart(Expert a, String free) {
        List<Expert> experts = null;
        if(free.equals("free")){
            experts = expertMapper.getFreeExpertsPart(a);
        }else{

        }
        return experts;
    }

    @Override
    public void savezj(ExpertsTemp expertsTemp) {
        expertMapper.savezj(expertsTemp);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void cqzj(List<Expert> experts, String flag, String paguid, String tdate, String time, SysUser session)throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
        String phone = "";//发送短信
        for(Expert expert :experts){
            //更新userextension表抽取的次数
            this.updateUserExtension(expert);
            //插入expertsmap表数据
            this.insertExpertsMap(expert);
            //插入zjbak表数据
            this.insertZjBak(expert,flag);
            //参加会议的发送短信通知
            if(expert.getIsleave()!=1){
                phone += expert.getPhone()+",";
            }
        }
        String proName = "";
        CheckInfo info = new CheckInfo();
        if("pssh".equals(flag)){
            //更新评审申报表状态
            Pssh s = pszlMapper.getDetailByPrjNo(paguid);
            proName = pszlMapper.getDetailByPrjNo(paguid).getProjectname();
            Pssh prj = new Pssh();
            prj.setGuid(paguid);//guid
            prj.setProjectstatus("3");//3:评审中-已抽取专家
            prj.setProjectstatus_cn("评审中");
            prj.setPsdate(tdate);//评审日期
            prj.setUpdatetime(new Date());
            pszlMapper.updateObjByGuid(prj);

            info.setCheckstep("抽取评审专家");
            info.setComments("抽取专家成功，流程至方案评审");

            operLogService.insertLog("抽取评审专家","_SaveZj","记录日志",s.getProjectno()+"项目 抽取评审专家 保存提交",session.getGuid());
        }else if("yssh".equals(flag)){
            //更新验收表状态
            Yssh s = ysshMapper.getObjByGuid(paguid);
            proName = ysshMapper.getObjByGuid(paguid).getProjectname();
            Yssh yssh =new Yssh();
            yssh.setGuid(paguid);//验收id
            yssh.setProjectstatus("3");
            yssh.setProjectstatus_cn("评审中");
            yssh.setYsdate(tdate);//验收日期
            yssh.setUpdatetime(new Date());
            ysshMapper.updateObjByGuid(yssh);

            info.setCheckstep("抽取验收专家");
            info.setComments("抽取专家成功，流程至竣工验收");

            operLogService.insertLog("抽取验收专家","_SelZjDetailList","记录日志",s.getProjectno()+"项目 抽取验收专家 保存提交",session.getGuid());
        }

        //审批信息表插入数据
        info.setPaguid(paguid);
        info.setCheckresult("抽取专家成功");
        info.setCheckuser(session.getGuid());//当前用户guid
        info.setCheckdate(new Date());
        info.setReturnnode("");
        expertMapper.insertCheckInfo(info);

        //删除临时表数据expertstemp
        expertMapper.deleteExpertsTemp(paguid);

        //发送短信通知
        String smsContent = "您好，苏州市公安局"+proName+"项目于"+sdf1.format(sdf.parse(tdate))+time+(flag.equals("pssh")?"评审":"验收")+"，能否准时参加，请及时回复。";
        int i = smsService.sendMsm( phone,  smsContent);
        //短信存入SMSRecords表
        Sms sms = new Sms();
        sms.setSmsno(smsService.getNewSmsNo());
        sms.setReceivephone(phone);
        sms.setRecipient("");
        sms.setSender(session.getRealname());
        sms.setSendtime(new Date());
        sms.setSmstype("0");//1通知类，0回访类
        sms.setSmstitle("");
        sms.setSmscontent(smsContent);
        sms.setSmsstate(i);//0发送失败，1发送成功， 2未发送
        sms.setIstiming("0");
        smsService.insertSMSRecords(sms);
    }
}
